package com.ruoyi.exam.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.exam.domain.ExamUserBankProgress;
import com.ruoyi.exam.vo.ExamUserBankProgressVo;
import com.ruoyi.exam.vo.ExamUserPracticeRecordVo;
import com.ruoyi.exam.vo.UserBankProgressAndRecordVo;
import com.ruoyi.exam.mapper.ExamUserBankProgressMapper;
import com.ruoyi.exam.service.IExamUserBankProgressService;
import com.ruoyi.exam.service.IExamUserPracticeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户题库进度Service业务层处理
 *
 * @author Tellsea
 * @date 2025-06-23
 */
@Service
public class ExamUserBankProgressServiceImpl extends ServiceImpl<ExamUserBankProgressMapper, ExamUserBankProgress> implements IExamUserBankProgressService {

    @Autowired
    private IExamUserPracticeRecordService examUserPracticeRecordService;


    @Override
    public TableDataInfo<ExamUserBankProgressVo> queryList(ExamUserBankProgressVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamUserBankProgressVo> queryAll(ExamUserBankProgressVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamUserBankProgressVo queryById(Long progressId) {
        return this.baseMapper.queryById(progressId);
    }

    @Override
    public ExamUserBankProgressVo getByUserIdAndBankId(Long userId, Long bankId) {
        return this.baseMapper.getByUserIdAndBankId(userId, bankId);
    }

    @Override
    public UserBankProgressAndRecordVo getUserProgressAndRecords(Long userId, Long bankId) {
        UserBankProgressAndRecordVo result = new UserBankProgressAndRecordVo();
        
        // 查询进度信息
        ExamUserBankProgressVo progress = getByUserIdAndBankId(userId, bankId);
        result.setProgress(progress);
        result.setHasProgress(progress != null);
        
        // 查询答题记录
        List<ExamUserPracticeRecordVo> records = examUserPracticeRecordService.getByUserIdAndBankId(userId, bankId);
        result.setPracticeRecords(records);
        result.setHasRecords(records != null && !records.isEmpty());

        return result;
    }
}
